package com.yt.sys.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.sys.models.bo.SysUserSaveBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.yt.sys.models.service.ISysUserService;
import com.yt.sys.models.bo.SysUserBO;
import com.yt.sys.models.dto.SysUserDTO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 登录信息控制器
 *
 * @author lizicong
 * @Date 2025-10-25 00:31:53
 */

@RestController
@RequestMapping("/api/sysuser")
@Slf4j
@Api(value = "SysUserApiController",tags = "登录信息Api接口")
public class SysUserApiController {

    @DubboReference
    private ISysUserService sysUserService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysUserDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        SysUserDTO dto = sysUserService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "登录信息列表",notes = "登录信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysUserDTO>> queryPageList(SysUserBO bo){
        Wrapper<SysUserDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<SysUserDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = sysUserService.selectPage(page, wrapper);

        return new Result<>(page);
    }
    @ApiOperation(value = "添加管理员信息",notes = "添加管理员信息")
    @PostMapping(value = "/insert")
    public Result insert(@RequestBody SysUserSaveBO saveBO) {
        boolean flag = sysUserService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改管理员信息",notes = "修改管理员信息")
    @PostMapping("/update")
    public Result update(@RequestBody SysUserSaveBO saveBO){
        SysUserDTO dto = new SysUserDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setUserId(saveBO.getUserId());
        boolean flag = sysUserService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "删除管理员信息",notes = "删除管理员信息")
    @ApiImplicitParam(name = "id",value = "管理员信息id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        boolean flag = sysUserService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @ApiOperation(value = "获取管理员信息列表",notes = "获取管理员信息列表")
    @PostMapping("/list")
    public Result<List<SysUserDTO>> getsysUserList(){
        System.out.println("获取列表检测点");
        List<SysUserDTO> list = sysUserService.getsysUserList();
        return new Result<>(list);
    }

    @ApiOperation(value = "获取管理员信息分页列表", notes = "获取管理员信息分页列表")
    @PostMapping("/pagelist/{page}")
    public Result<Map<String, Object>> getsysUserListpage(@PathVariable("page") int page) {
        log.info("获取分页管理员信息列表，页码：{}", page);

        // 1. 调用 Service 获取分页数据
        Page<SysUserDTO> sysUserDTOPage = sysUserService.getpagesysUserList(page);

        // 2. 生成页码列表
        int pages = (int) sysUserDTOPage.getPages();
        List<Integer> pageList = IntStream.rangeClosed(1, pages)
                .boxed()
                .collect(Collectors.toList());

        // 3. 用 Map 封装所有信息
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("records", sysUserDTOPage.getRecords()); // 当前页数据
        resultMap.put("current", sysUserDTOPage.getCurrent()); // 当前页码
        resultMap.put("size", sysUserDTOPage.getSize()); // 每页条数
        resultMap.put("total", sysUserDTOPage.getTotal()); // 总记录数
        resultMap.put("pages", pages); // 总页数
        resultMap.put("pageList", pageList); // 页码列表

        return new Result<>(resultMap);
    }

    @ApiOperation(value = "根据关键词获取管理员信息列表",notes = "根据关键词获取管理员信息列表")
    @PostMapping("/search/{keyword}")
    public Result<List<SysUserDTO>> searchgetsysUserList(@PathVariable("keyword") String keyword){
        System.out.println("获取列表检测点");
        List<SysUserDTO> list = sysUserService.searchgetsysUserList(keyword);
        return new Result<>(list);
    }
}
